/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jardownloader;

import java.awt.Color;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;

/**
 *
 * @author BB
 */
public class JarDownloaderFrame extends javax.swing.JFrame {

    String PATH_WIN = System.getProperty("user.home") + "\\AppData\\Roaming\\.minecraft\\bin\\minecraft.jar";
    String PATH_MAC = System.getProperty("user.home") + "/Application Support/minecraft/bin/minecraft.jar";
    String PATH_LINUX = System.getProperty("user.home");
    static String CURRENT_OS = System.getProperty("os.name");
    ImageIcon image;

    /**
     * Creates new form JarDownloaderFrame
     */
    void downloadLogo() {
        try {
            // Create a URL for the image's location
            URL url = new URL("http://minecraft.de/customavatars/avatar61706_1.gif");

            // Get the image
            image = new ImageIcon(url);
        } catch (MalformedURLException e) {
        }
    }

    public JarDownloaderFrame() {
        initComponents();
        downloadLogo();
        imageLabel.setIcon(image);
        progressLabel.setText(CURRENT_OS);
        System.err.println(PATH_WIN);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        downloadButton = new javax.swing.JButton();
        progressLabel = new javax.swing.JLabel();
        imageLabel = new javax.swing.JLabel();
        headLabel = new javax.swing.JLabel();
        checkForNew = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Blockprovinz .jar Downloader v0.1");

        downloadButton.setText("Download");
        downloadButton.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                downloadButtonActionPerformed(evt);
            }
        });

        progressLabel.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        progressLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        progressLabel.setText("Idle");

        imageLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

        headLabel.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        headLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        headLabel.setText("Blockprovinz .jar Downloader v0.1");

        checkForNew.setText("Check");
        checkForNew.setMaximumSize(new java.awt.Dimension(79, 23));
        checkForNew.setMinimumSize(new java.awt.Dimension(79, 23));
        checkForNew.setPreferredSize(new java.awt.Dimension(79, 23));
        checkForNew.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                checkForNewActionPerformed(evt);
            }
        });

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Status");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(headLabel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 333, Short.MAX_VALUE).addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(imageLabel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addComponent(progressLabel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addComponent(checkForNew, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addComponent(downloadButton)).addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))).addContainerGap()));
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addContainerGap().addComponent(headLabel).addGap(26, 26, 26).addComponent(imageLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 34, Short.MAX_VALUE).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(downloadButton).addComponent(checkForNew, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)).addGap(19, 19, 19).addComponent(jLabel1).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED).addComponent(progressLabel).addGap(24, 24, 24)));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void downloadButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_downloadButtonActionPerformed
        String os = CURRENT_OS;
        File f = null;
        Downloader d;

        if (os.equalsIgnoreCase("windows 7") || os.equalsIgnoreCase("windows xp") || os.equalsIgnoreCase("windows nt") || os.equalsIgnoreCase("windows vista")) {
            d = new Downloader(PATH_WIN);
        } else if (os.contains("mac")) {
            d = new Downloader(PATH_MAC);
        } else {
            d = new Downloader(PATH_LINUX);
        }
        d.getFile();
        progressLabel.setForeground(Color.GREEN);
        progressLabel.setText("Done");
    }//GEN-LAST:event_downloadButtonActionPerformed

    private void checkForNewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_checkForNewActionPerformed
        try {
            String url = "http://dl.dropbox.com/u/30341483/Minecraft/Normal/minecraft.jar";
            Checker c = new Checker();
            long date = c.checkFile(url);
            String os = CURRENT_OS;
            File f = null;
            if (os.equalsIgnoreCase("windows 7") || os.equalsIgnoreCase("windows 8") || os.equalsIgnoreCase("windows xp") || os.equalsIgnoreCase("windows nt") || os.equalsIgnoreCase("windows vista")) {
                f = new File(PATH_WIN);
            } else if (os.contains("mac")) {
                f = new File(PATH_MAC);
            } else {
                f = new File(PATH_LINUX);
            }

            if (!f.exists()) {
                progressLabel.setText("New Version!");
                progressLabel.setForeground(Color.GREEN);
            } else if (f.exists() && f.lastModified() < date) {
                progressLabel.setText("New Version!");
                progressLabel.setForeground(Color.GREEN);
            } else {
                progressLabel.setText("You already got it!");
                progressLabel.setForeground(Color.RED);
            }
        } catch (ProtocolException ex) {
            Logger.getLogger(JarDownloaderFrame.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(JarDownloaderFrame.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalStateException ex) {
            Logger.getLogger(JarDownloaderFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_checkForNewActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(JarDownloaderFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(JarDownloaderFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(JarDownloaderFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(JarDownloaderFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new JarDownloaderFrame().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton checkForNew;
    private javax.swing.JButton downloadButton;
    private javax.swing.JLabel headLabel;
    private javax.swing.JLabel imageLabel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel progressLabel;
    // End of variables declaration//GEN-END:variables
}
